import 'dart:math';

import 'package:flutter/material.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/entity/device_mine_entity.dart';
import 'package:oramemo/generated/assets.dart';
import 'package:oramemo/pages/home/home_logic.dart';

class DeviceCard extends StatelessWidget {
  final DeviceMineData device;
  final VoidCallback? onTap;

  const DeviceCard({
    super.key,
    required this.device,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final isCurrent = device.device?.id == HomeLogic.to.currentDeviceId;
    // 1自定义，2强降噪，3室内，4室外
    Map<int, String> modeJson = {1: '自定义', 2: '强降噪', 3: '室内', 4: '室外'};
    final String deviceModelName = modeJson[device.device?.deviceModel] ?? '';
    final batteryLevel = 0;
    final String permissionName = device.devicePermission==1?'管理员':'成员';
    final isBlueConnected = false;
    return Container(
      height: 180.w,
      alignment: Alignment.center,
      margin: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
      padding: EdgeInsets.fromLTRB(0, 0, 0, 4.w),
      decoration: ShapeDecoration(
        color: const Color(0xFFF3F5F9),
        shape: RoundedRectangleBorder(
          side: const BorderSide(width: 1, color: Color(0x1992B9FF)),
          borderRadius: BorderRadius.circular(36.r),
        ),
      ),
      child: InkWell(
        onTap: onTap,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            // 设备图片
            Container(
              width: 124.w,
              height: 148.w,
              margin: EdgeInsets.only(left: 27.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(36.r),
                image: DecorationImage(
                  image: AssetImage(Assets.facilityHardware),
                  fit: BoxFit.cover,
                ),
              ),
            ),
            SizedBox(width: 22.w),
            // 设备信息
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      Expanded(
                        child: Text(
                          device.device?.deviceName ?? '',
                          style: TextStyle(
                            fontSize: 20.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 16.h),
                  // 状态指示器行
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisSize: MainAxisSize.max,
                        children: [
                          // 蓝牙状态
                          _buildStatusItem(
                            icon: IconX.image(
                                isBlueConnected
                                    ? Assets.facilityBle
                                    : Assets.facilityBleNot,
                                size: 18),
                            text: isBlueConnected ? '已连接' : '未连接',
                          ),
                          const Spacer(),
                          // 电池状态
                          _buildStatusItem(
                            icon: Padding(
                              padding: EdgeInsets.only(right: 4.0),
                              child: IconX.image(
                                  Assets.facilityBaltter0.replaceFirst(
                                      '0', (batteryLevel % 10).toString()),
                                  width: 22,
                                  height: 14),
                            ),
                            text: '${batteryLevel}%',
                          ),
                          const Spacer(),
                          // 信号强度
                          _buildStatusItem(
                            icon: const Padding(
                              padding: EdgeInsets.only(right:3.0),
                              child: IconX.image(Assets.facilityListening,
                                  size: 18),
                            ),
                            text: deviceModelName,
                          ),
                        ],
                      ),
                      SizedBox(height: 13.h),
                      // 底部按钮和权限
                      Text(
                        '设备权限：${permissionName}',
                        style: TextStyle(
                          color: Color(0xFF555555),
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                      SizedBox(height: 21.h),
                      // 当前设备按钮
                      if (isCurrent)
                        Container(
                          width: 120.w,
                          height: 30.h,
                          padding: EdgeInsets.only(bottom: 1.w),
                          decoration: ShapeDecoration(
                            color: Color(0xFF2574FF),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12.r),
                            ),
                          ),
                          alignment: Alignment.center,
                          child: Text(
                            '当前设备',
                            style:
                                TextStyle(color: Colors.white, fontSize: 14.sp),
                          ),
                        ),
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(
              width: 12,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatusItem({
    required Widget icon,
    required String text,
  }) {
    return Row(
      children: [
        icon,
        Text(
          text,
          style: TextStyle(
            color: Colours.text,
            fontSize: 12.sp,
          ),
        ),
      ],
    );
  }
}
